
#include "klotski.h"
#include "ui_klotski.h"
#include<QPainter>
#include<QPixmap>
#include<QTimer>
#include<QKeyEvent>
#include<QPushButton>
#include<QSound>

Klotski::Klotski(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Klotski),isrun(false),Direction(0),role(-1),steps(0),iswin(false),isSound(true)
{
    ui->setupUi(this);
    this->setGeometry(QRect(270,80,1500,900));
    setWindowTitle("华容道");
    setFixedSize(1500,900);
    QPushButton *btn=new QPushButton;
    btn->setParent(this);
    btn->resize(100,35);
    QFont font0("STXinwei",15);
    btn->setFont(font0);
    btn->move(1210,625);
    btn->setText("重新开始");
    connect(btn,SIGNAL(clicked()),SLOT(restart()));
    QPushButton *btn1=new QPushButton;
    btn1->setParent(this);
    btn1->resize(100,35);
    btn1->setFont(font0);
    btn1->move(1210,705);
    btn1->setText("开启音效");
    connect(btn1,SIGNAL(clicked()),SLOT(openSound()));
    QPushButton *btn2=new QPushButton;
    btn2->setParent(this);
    btn2->resize(100,35);
    btn2->setFont(font0);
    btn2->move(1210,785);
    btn2->setText("关闭音效");
    connect(btn2,SIGNAL(clicked()),SLOT(closeSound()));
}

Klotski::~Klotski()
{
    delete ui;
}
void Klotski::paintEvent(QPaintEvent *event)
{

   if(!isrun)
   {
    Initcc();
    Initzf();
    Initgy();
    Initmc();
    Initzy();
    Inithz();
    Initb();
    Init();
   }
    QPainter painter(this);

    //背景
    painter.drawPixmap(0,0,1500,900,QPixmap(":/666/pic/底板.jpg"));
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawRect(leftb);
    painter.drawRect(topb);
    painter.drawRect(bottomb);
    painter.drawRect(rightb1);
    painter.drawRect(rightb2);
    painter.drawPixmap(15,65,1000,800,QPixmap(":/666/pic/背景.jpg"));
    QFont font1("STXinwei",100);
    painter.setFont(font1);
    painter.setPen(Qt::darkRed);
    painter.setBrush(Qt::darkRed);
    painter.drawText(1180,180,"华");
    painter.drawText(1180,330,"容");
    painter.drawText(1180,480,"道");
    QFont font2("Times New Roman",25);
    painter.setFont(font2);
    painter.setPen(Qt::yellow);
    painter.setBrush(Qt::yellow);
    painter.drawText(1280,560,"Steps:");
    painter.drawText(1400,560,QString::number(steps));

    //画人物
    painter.drawRect(Zf);
    painter.drawPixmap(Zf,QPixmap(":/666/pic/张飞.png"));
    painter.drawRect(Gy);
    painter.drawPixmap(Gy,QPixmap(":/666/pic/关羽.png"));
    painter.drawRect(Zy);
    painter.drawPixmap(Zy,QPixmap(":/666/pic/赵云.png"));
    painter.drawRect(Hz);
    painter.drawPixmap(Hz,QPixmap(":/666/pic/黄忠.png"));
    painter.drawRect(Mc);
    painter.drawPixmap(Mc,QPixmap(":/666/pic/马超.png"));
    painter.drawRect(Cc);
    painter.drawPixmap(Cc,QPixmap(":/666/pic/曹操.png"));
    painter.drawRect(B1);
    painter.drawPixmap(B1,QPixmap(":/666/pic/小兵1.png"));
    painter.drawRect(B2);
    painter.drawPixmap(B2,QPixmap(":/666/pic/小兵2.png"));
    painter.drawRect(B3);
    painter.drawPixmap(B3,QPixmap(":/666/pic/小兵3.png"));
    painter.drawRect(B4);
    painter.drawPixmap(B4,QPixmap(":/666/pic/小兵4.png"));
    if(Cc.intersects(win))
    {
        QSound *sl=new QSound(":/666/pic/胜利.wav");
        iswin=true;
        sl->play();
        painter.setFont(font1);
        painter.setPen(Qt::cyan);
        painter.setBrush(Qt::cyan);
        painter.drawText(180,425,"呃哦，曹贼溜了！");
    }
    if(isrun)
            timer->stop();
}
void Klotski::Init()
{
    QRect rect8(15,50,1015,15),rect2(15,865,1015,15),rect4(0,50,15,830),rect9(1015,65,15,200),rect3(1015,665,15,200),rect6(1015,265,15,400),rectw(1030,265,15,400);
    rightb1=rect9;
    rightb2=rect3;
    leftb=rect4;
    topb=rect8;
    bottomb=rect2;
    b=rect6;
    win=rectw;
}
void Klotski::Initzf()
{
    QRect rect(15,665,400,200);
    Zf=rect;
}
void Klotski::Initcc()
{
    QRect rect(15,265,400,400);
    Cc=rect;
    isrun=true;
}
void Klotski::Initmc()
{
    QRect rect(15,65,400,200);
    Mc=rect;
}
void Klotski::Inithz()
{
    QRect rect(415,665,400,200);
    Hz=rect;
}
void Klotski::Initgy()
{
    QRect rect(615,265,200,400);
    Gy=rect;
}
void Klotski::Initzy()
{
    QRect rect(415,65,400,200);
    Zy=rect;
}
void Klotski::Initb()
{
     QRect rect1(815,65,200,200),rect2(815,265,200,200),rect3(815,465,200,200),rect4(815,665,200,200);
     B1=rect1;
     B2=rect2;
     B3=rect3;
     B4=rect4;
}
void Klotski::k_update()
{
    switch(role)
   {
   case 0:                 //小兵4
        switch(Direction)
        {
        case 8:
            B4.setTop(B4.top()-200);
            B4.setBottom(B4.bottom()-200);
            if(B4.intersects(B2)||B4.intersects(Zf)||B4.intersects(Mc)||B4.intersects(Cc)||B4.intersects(Hz)||B4.intersects(Zy)||B4.intersects(Gy)||B4.intersects(B3)||B4.intersects(B1)||B4.intersects(leftb)||B4.intersects(topb)||B4.intersects(bottomb)||B4.intersects(rightb1)||B4.intersects(rightb2)||B4.intersects(b))
            {
                B4.setTop(B4.top()+200);
                B4.setBottom(B4.bottom()+200);
                steps--;
            }
            break;
        case 2:
            B4.setTop(B4.top()+200);
            B4.setBottom(B4.bottom()+200);
            if(B4.intersects(B2)||B4.intersects(Zf)||B4.intersects(Mc)||B4.intersects(Cc)||B4.intersects(Hz)||B4.intersects(Zy)||B4.intersects(Gy)||B4.intersects(B3)||B4.intersects(B1)||B4.intersects(leftb)||B4.intersects(topb)||B4.intersects(bottomb)||B4.intersects(rightb1)||B4.intersects(rightb2)||B4.intersects(b))
            {
                B4.setTop(B4.top()-200);
                B4.setBottom(B4.bottom()-200);
                steps--;
            }
            break;
        case 4:
            B4.setLeft(B4.left()-200);
            B4.setRight(B4.right()-200);
            if(B4.intersects(B2)||B4.intersects(Zf)||B4.intersects(Mc)||B4.intersects(Cc)||B4.intersects(Hz)||B4.intersects(Zy)||B4.intersects(Gy)||B4.intersects(B3)||B4.intersects(B1)||B4.intersects(leftb)||B4.intersects(topb)||B4.intersects(bottomb)||B4.intersects(rightb1)||B4.intersects(rightb2)||B4.intersects(b))
            {
                B4.setLeft(B4.left()+200);
                B4.setRight(B4.right()+200);
                steps--;
            }
            break;
        case 6:
            B4.setLeft(B4.left()+200);
            B4.setRight(B4.right()+200);
            if(B4.intersects(B2)||B4.intersects(Zf)||B4.intersects(Mc)||B4.intersects(Cc)||B4.intersects(Hz)||B4.intersects(Zy)||B4.intersects(Gy)||B4.intersects(B3)||B4.intersects(B1)||B4.intersects(leftb)||B4.intersects(topb)||B4.intersects(bottomb)||B4.intersects(rightb1)||B4.intersects(rightb2)||B4.intersects(b))
            {
                B4.setLeft(B4.left()-200);
                B4.setRight(B4.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
    case 1:                //张飞
        switch(Direction)
        {
        case 8:
            Zf.setTop(Zf.top()-200);
            Zf.setBottom(Zf.bottom()-200);
            if(Zf.intersects(B2)||Zf.intersects(B4)||Zf.intersects(Mc)||Zf.intersects(Cc)||Zf.intersects(Hz)||Zf.intersects(Zy)||Zf.intersects(Gy)||Zf.intersects(B3)||Zf.intersects(B1)||Zf.intersects(leftb)||Zf.intersects(topb)||Zf.intersects(bottomb)||Zf.intersects(rightb1)||Zf.intersects(rightb2)||Zf.intersects(b))
            {
                Zf.setTop(Zf.top()+200);
                Zf.setBottom(Zf.bottom()+200);
                steps--;
            }
            break;
        case 2:
            Zf.setTop(Zf.top()+200);
            Zf.setBottom(Zf.bottom()+200);
            if(Zf.intersects(B2)||Zf.intersects(B4)||Zf.intersects(Mc)||Zf.intersects(Cc)||Zf.intersects(Hz)||Zf.intersects(Zy)||Zf.intersects(Gy)||Zf.intersects(B3)||Zf.intersects(B1)||Zf.intersects(leftb)||Zf.intersects(topb)||Zf.intersects(bottomb)||Zf.intersects(rightb1)||Zf.intersects(rightb2)||Zf.intersects(b))
            {
                Zf.setTop(Zf.top()-200);
                Zf.setBottom(Zf.bottom()-200);
                steps--;
            }
            break;
        case 4:
            Zf.setLeft(Zf.left()-200);
            Zf.setRight(Zf.right()-200);
            if(Zf.intersects(B2)||Zf.intersects(B4)||Zf.intersects(Mc)||Zf.intersects(Cc)||Zf.intersects(Hz)||Zf.intersects(Zy)||Zf.intersects(Gy)||Zf.intersects(B3)||Zf.intersects(B1)||Zf.intersects(leftb)||Zf.intersects(topb)||Zf.intersects(bottomb)||Zf.intersects(rightb1)||Zf.intersects(rightb2)||Zf.intersects(b))
            {
                Zf.setLeft(Zf.left()+200);
                Zf.setRight(Zf.right()+200);
                steps--;
            }
            break;
        case 6:
            Zf.setLeft(Zf.left()+200);
            Zf.setRight(Zf.right()+200);
            if(Zf.intersects(B2)||Zf.intersects(B4)||Zf.intersects(Mc)||Zf.intersects(Cc)||Zf.intersects(Hz)||Zf.intersects(Zy)||Zf.intersects(Gy)||Zf.intersects(B3)||Zf.intersects(B1)||Zf.intersects(leftb)||Zf.intersects(topb)||Zf.intersects(bottomb)||Zf.intersects(rightb1)||Zf.intersects(rightb2)||Zf.intersects(b))
            {
                Zf.setLeft(Zf.left()-200);
                Zf.setRight(Zf.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
    case 2:                //黄忠
        switch(Direction)
        {
        case 8:
            Hz.setTop(Hz.top()-200);
            Hz.setBottom(Hz.bottom()-200);
            if(Hz.intersects(B2)||Hz.intersects(B4)||Hz.intersects(Mc)||Hz.intersects(Cc)||Hz.intersects(Zf)||Hz.intersects(Zy)||Hz.intersects(Gy)||Hz.intersects(B3)||Hz.intersects(B1)||Hz.intersects(leftb)||Hz.intersects(topb)||Hz.intersects(bottomb)||Hz.intersects(rightb1)||Hz.intersects(rightb2)||Hz.intersects(b))
            {
                Hz.setTop(Hz.top()+200);
                Hz.setBottom(Hz.bottom()+200);
                steps--;
            }
            break;
        case 2:
            Hz.setTop(Hz.top()+200);
            Hz.setBottom(Hz.bottom()+200);
            if(Hz.intersects(B2)||Hz.intersects(B4)||Hz.intersects(Mc)||Hz.intersects(Cc)||Hz.intersects(Zf)||Hz.intersects(Zy)||Hz.intersects(Gy)||Hz.intersects(B3)||Hz.intersects(B1)||Hz.intersects(leftb)||Hz.intersects(topb)||Hz.intersects(bottomb)||Hz.intersects(rightb1)||Hz.intersects(rightb2)||Hz.intersects(b))
            {
                Hz.setTop(Hz.top()-200);
                Hz.setBottom(Hz.bottom()-200);
                steps--;
            }
            break;
        case 4:
            Hz.setLeft(Hz.left()-200);
            Hz.setRight(Hz.right()-200);
            if(Hz.intersects(B2)||Hz.intersects(B4)||Hz.intersects(Mc)||Hz.intersects(Cc)||Hz.intersects(Zf)||Hz.intersects(Zy)||Hz.intersects(Gy)||Hz.intersects(B3)||Hz.intersects(B1)||Hz.intersects(leftb)||Hz.intersects(topb)||Hz.intersects(bottomb)||Hz.intersects(rightb1)||Hz.intersects(rightb2)||Hz.intersects(b))
            {
                Hz.setLeft(Hz.left()+200);
                Hz.setRight(Hz.right()+200);
                steps--;
            }
            break;
        case 6:
            Hz.setLeft(Hz.left()+200);
            Hz.setRight(Hz.right()+200);
            if(Hz.intersects(B2)||Hz.intersects(B4)||Hz.intersects(Mc)||Hz.intersects(Cc)||Hz.intersects(Zf)||Hz.intersects(Zy)||Hz.intersects(Gy)||Hz.intersects(B3)||Hz.intersects(B1)||Hz.intersects(leftb)||Hz.intersects(topb)||Hz.intersects(bottomb)||Hz.intersects(rightb1)||Hz.intersects(rightb2)||Hz.intersects(b))
            {
                Hz.setLeft(Hz.left()-200);
                Hz.setRight(Hz.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
    case 3:                 //小兵3
        switch(Direction)
        {
        case 8:
            B3.setTop(B3.top()-200);
            B3.setBottom(B3.bottom()-200);
            if(B3.intersects(B2)||B3.intersects(B4)||B3.intersects(Mc)||B3.intersects(Cc)||B3.intersects(Zf)||B3.intersects(Zy)||B3.intersects(Gy)||B3.intersects(Hz)||B3.intersects(B1)||B3.intersects(leftb)||B3.intersects(topb)||B3.intersects(bottomb)||B3.intersects(rightb1)||B3.intersects(rightb2)||B3.intersects(b))
            {
                B3.setTop(B3.top()+200);
                B3.setBottom(B3.bottom()+200);
                steps--;
            }
            break;
        case 2:
            B3.setTop(B3.top()+200);
            B3.setBottom(B3.bottom()+200);
            if(B3.intersects(B2)||B3.intersects(B4)||B3.intersects(Mc)||B3.intersects(Cc)||B3.intersects(Zf)||B3.intersects(Zy)||B3.intersects(Gy)||B3.intersects(Hz)||B3.intersects(B1)||B3.intersects(leftb)||B3.intersects(topb)||B3.intersects(bottomb)||B3.intersects(rightb1)||B3.intersects(rightb2)||B3.intersects(b))
            {
                B3.setTop(B3.top()-200);
                B3.setBottom(B3.bottom()-200);
                steps--;
            }
            break;
        case 4:
            B3.setLeft(B3.left()-200);
            B3.setRight(B3.right()-200);
            if(B3.intersects(B2)||B3.intersects(B4)||B3.intersects(Mc)||B3.intersects(Cc)||B3.intersects(Zf)||B3.intersects(Zy)||B3.intersects(Gy)||B3.intersects(Hz)||B3.intersects(B1)||B3.intersects(leftb)||B3.intersects(topb)||B3.intersects(bottomb)||B3.intersects(rightb1)||B3.intersects(rightb2)||B3.intersects(b))
            {
                B3.setLeft(B3.left()+200);
                B3.setRight(B3.right()+200);
                steps--;
            }
            break;
        case 6:
            B3.setLeft(B3.left()+200);
            B3.setRight(B3.right()+200);
            if(B3.intersects(B2)||B3.intersects(B4)||B3.intersects(Mc)||B3.intersects(Cc)||B3.intersects(Zf)||B3.intersects(Zy)||B3.intersects(Gy)||B3.intersects(Hz)||B3.intersects(B1)||B3.intersects(leftb)||B3.intersects(topb)||B3.intersects(bottomb)||B3.intersects(rightb1)||B3.intersects(rightb2)||B3.intersects(b))
            {
                B3.setLeft(B3.left()-200);
                B3.setRight(B3.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
    case 4:                  //曹操
        switch(Direction)
        {
        case 8:
            Cc.setTop(Cc.top()-200);
            Cc.setBottom(Cc.bottom()-200);
            if(Cc.intersects(Mc)||Cc.intersects(Zf)||Cc.intersects(Zy)||Cc.intersects(Gy)||Cc.intersects(Hz)||Cc.intersects(B1)||Cc.intersects(B2)||Cc.intersects(B3)||Cc.intersects(B4)||Cc.intersects(leftb)||Cc.intersects(topb)||Cc.intersects(bottomb)||Cc.intersects(rightb1)||Cc.intersects(rightb2))
            {
                Cc.setTop(Cc.top()+200);
                Cc.setBottom(Cc.bottom()+200);
                steps--;
            }
                break;
        case 2:
            Cc.setTop(Cc.top()+200);
            Cc.setBottom(Cc.bottom()+200);
            if(Cc.intersects(Mc)||Cc.intersects(Zf)||Cc.intersects(Zy)||Cc.intersects(Gy)||Cc.intersects(Hz)||Cc.intersects(B1)||Cc.intersects(B2)||Cc.intersects(B3)||Cc.intersects(B4)||Cc.intersects(leftb)||Cc.intersects(topb)||Cc.intersects(bottomb)||Cc.intersects(rightb1)||Cc.intersects(rightb2))
            {
                Cc.setTop(Cc.top()-200);
                Cc.setBottom(Cc.bottom()-200);
                steps--;
            }
            break;
        case 4:
            Cc.setLeft(Cc.left()-200);
            Cc.setRight(Cc.right()-200);
            if(Cc.intersects(Mc)||Cc.intersects(Zf)||Cc.intersects(Zy)||Cc.intersects(Gy)||Cc.intersects(Hz)||Cc.intersects(B1)||Cc.intersects(B2)||Cc.intersects(B3)||Cc.intersects(B4)||Cc.intersects(leftb)||Cc.intersects(topb)||Cc.intersects(bottomb)||Cc.intersects(rightb1)||Cc.intersects(rightb2))
            {
                Cc.setLeft(Cc.left()+200);
                Cc.setRight(Cc.right()+200);
                steps--;
            }
            break;
        case 6:
            Cc.setLeft(Cc.left()+200);
            Cc.setRight(Cc.right()+200);
            if(Cc.intersects(Mc)||Cc.intersects(Zf)||Cc.intersects(Zy)||Cc.intersects(Gy)||Cc.intersects(Hz)||Cc.intersects(B1)||Cc.intersects(B2)||Cc.intersects(B3)||Cc.intersects(B4)||Cc.intersects(leftb)||Cc.intersects(topb)||Cc.intersects(bottomb)||Cc.intersects(rightb1)||Cc.intersects(rightb2))
            {
                Cc.setLeft(Cc.left()-200);
                Cc.setRight(Cc.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
    case 5:                   //关羽
        switch(Direction)
        {
        case 8:
            Gy.setTop(Gy.top()-200);
            Gy.setBottom(Gy.bottom()-200);
            if(Gy.intersects(Mc)||Gy.intersects(Zf)||Gy.intersects(Zy)||Gy.intersects(Cc)||Gy.intersects(Hz)||Gy.intersects(B1)||Gy.intersects(B2)||Gy.intersects(B3)||Gy.intersects(B4)||Gy.intersects(leftb)||Gy.intersects(topb)||Gy.intersects(bottomb)||Gy.intersects(rightb1)||Gy.intersects(rightb2)||Gy.intersects(b))
            {
                Gy.setTop(Gy.top()+200);
                Gy.setBottom(Gy.bottom()+200);
                steps--;
            }
            break;
        case 2:
            Gy.setTop(Gy.top()+200);
            Gy.setBottom(Gy.bottom()+200);
            if(Gy.intersects(Mc)||Gy.intersects(Zf)||Gy.intersects(Zy)||Gy.intersects(Cc)||Gy.intersects(Hz)||Gy.intersects(B1)||Gy.intersects(B2)||Gy.intersects(B3)||Gy.intersects(B4)||Gy.intersects(leftb)||Gy.intersects(topb)||Gy.intersects(bottomb)||Gy.intersects(rightb1)||Gy.intersects(rightb2)||Gy.intersects(b))
            {
                Gy.setTop(Gy.top()-200);
                Gy.setBottom(Gy.bottom()-200);
                steps--;
            }
            break;
        case 4:
            Gy.setLeft(Gy.left()-200);
            Gy.setRight(Gy.right()-200);
            if(Gy.intersects(Mc)||Gy.intersects(Zf)||Gy.intersects(Zy)||Gy.intersects(Cc)||Gy.intersects(Hz)||Gy.intersects(B1)||Gy.intersects(B2)||Gy.intersects(B3)||Gy.intersects(B4)||Gy.intersects(leftb)||Gy.intersects(topb)||Gy.intersects(bottomb)||Gy.intersects(rightb1)||Gy.intersects(rightb2)||Gy.intersects(b))
            {
                Gy.setLeft(Gy.left()+200);
                Gy.setRight(Gy.right()+200);
                steps--;
            }
            break;
        case 6:
            Gy.setLeft(Gy.left()+200);
            Gy.setRight(Gy.right()+200);
            if(Gy.intersects(Mc)||Gy.intersects(Zf)||Gy.intersects(Zy)||Gy.intersects(Cc)||Gy.intersects(Hz)||Gy.intersects(B1)||Gy.intersects(B2)||Gy.intersects(B3)||Gy.intersects(B4)||Gy.intersects(leftb)||Gy.intersects(topb)||Gy.intersects(bottomb)||Gy.intersects(rightb1)||Gy.intersects(rightb2)||Gy.intersects(b))
            {
                Gy.setLeft(Gy.left()-200);
                Gy.setRight(Gy.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
    case 6:                  //小兵2
        switch(Direction)
        {
        case 8:
            B2.setTop(B2.top()-200);
            B2.setBottom(B2.bottom()-200);
            if(B2.intersects(Mc)||B2.intersects(Zf)||B2.intersects(Zy)||B2.intersects(Cc)||B2.intersects(Hz)||B2.intersects(B1)||B2.intersects(Gy)||B2.intersects(B3)||B2.intersects(B4)||B2.intersects(leftb)||B2.intersects(topb)||B2.intersects(bottomb)||B2.intersects(rightb1)||B2.intersects(rightb2)||B2.intersects(b))
            {
                B2.setTop(B2.top()+200);
                B2.setBottom(B2.bottom()+200);
                steps--;
            }
            break;
        case 2:
            B2.setTop(B2.top()+200);
            B2.setBottom(B2.bottom()+200);
            if(B2.intersects(Mc)||B2.intersects(Zf)||B2.intersects(Zy)||B2.intersects(Cc)||B2.intersects(Hz)||B2.intersects(B1)||B2.intersects(Gy)||B2.intersects(B3)||B2.intersects(B4)||B2.intersects(leftb)||B2.intersects(topb)||B2.intersects(bottomb)||B2.intersects(rightb1)||B2.intersects(rightb2)||B2.intersects(b))
            {
                B2.setTop(B2.top()-200);
                B2.setBottom(B2.bottom()-200);
                steps--;
            }
            break;
        case 4:
            B2.setLeft(B2.left()-200);
            B2.setRight(B2.right()-200);
            if(B2.intersects(Mc)||B2.intersects(Zf)||B2.intersects(Zy)||B2.intersects(Cc)||B2.intersects(Hz)||B2.intersects(B1)||B2.intersects(Gy)||B2.intersects(B3)||B2.intersects(B4)||B2.intersects(leftb)||B2.intersects(topb)||B2.intersects(bottomb)||B2.intersects(rightb1)||B2.intersects(rightb2)||B2.intersects(b))
            {
                B2.setLeft(B2.left()+200);
                B2.setRight(B2.right()+200);
                steps--;
            }
            break;
        case 6:
            B2.setLeft(B2.left()+200);
            B2.setRight(B2.right()+200);
            if(B2.intersects(Mc)||B2.intersects(Zf)||B2.intersects(Zy)||B2.intersects(Cc)||B2.intersects(Hz)||B2.intersects(B1)||B2.intersects(Gy)||B2.intersects(B3)||B2.intersects(B4)||B2.intersects(leftb)||B2.intersects(topb)||B2.intersects(bottomb)||B2.intersects(rightb1)||B2.intersects(rightb2)||B2.intersects(b))
            {
                B2.setLeft(B2.left()-200);
                B2.setRight(B2.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
    case 7:                //马超
        switch(Direction)
        {
        case 8:
            Mc.setTop(Mc.top()-200);
            Mc.setBottom(Mc.bottom()-200);
            if(Mc.intersects(B2)||Mc.intersects(Zf)||Mc.intersects(Zy)||Mc.intersects(Cc)||Mc.intersects(Hz)||Mc.intersects(B1)||Mc.intersects(Gy)||Mc.intersects(B3)||Mc.intersects(B4)||Mc.intersects(leftb)||Mc.intersects(topb)||Mc.intersects(bottomb)||Mc.intersects(rightb1)||Mc.intersects(rightb2)||Mc.intersects(b))
            {
                Mc.setTop(Mc.top()+200);
                Mc.setBottom(Mc.bottom()+200);
                steps--;
            }
            break;
        case 2:
            Mc.setTop(Mc.top()+200);
            Mc.setBottom(Mc.bottom()+200);
            if(Mc.intersects(B2)||Mc.intersects(Zf)||Mc.intersects(Zy)||Mc.intersects(Cc)||Mc.intersects(Hz)||Mc.intersects(B1)||Mc.intersects(Gy)||Mc.intersects(B3)||Mc.intersects(B4)||Mc.intersects(leftb)||Mc.intersects(topb)||Mc.intersects(bottomb)||Mc.intersects(rightb1)||Mc.intersects(rightb2)||Mc.intersects(b))
            {
                Mc.setTop(Mc.top()-200);
                Mc.setBottom(Mc.bottom()-200);
                steps--;
            }
            break;
        case 4:
            Mc.setLeft(Mc.left()-200);
            Mc.setRight(Mc.right()-200);
            if(Mc.intersects(B2)||Mc.intersects(Zf)||Mc.intersects(Zy)||Mc.intersects(Cc)||Mc.intersects(Hz)||Mc.intersects(B1)||Mc.intersects(Gy)||Mc.intersects(B3)||Mc.intersects(B4)||Mc.intersects(leftb)||Mc.intersects(topb)||Mc.intersects(bottomb)||Mc.intersects(rightb1)||Mc.intersects(rightb2)||Mc.intersects(b))
            {
                Mc.setLeft(Mc.left()+200);
                Mc.setRight(Mc.right()+200);
                steps--;
            }
            break;
        case 6:
            Mc.setLeft(Mc.left()+200);
            Mc.setRight(Mc.right()+200);
            if(Mc.intersects(B2)||Mc.intersects(Zf)||Mc.intersects(Zy)||Mc.intersects(Cc)||Mc.intersects(Hz)||Mc.intersects(B1)||Mc.intersects(Gy)||Mc.intersects(B3)||Mc.intersects(B4)||Mc.intersects(leftb)||Mc.intersects(topb)||Mc.intersects(bottomb)||Mc.intersects(rightb1)||Mc.intersects(rightb2)||Mc.intersects(b))
            {
                Mc.setLeft(Mc.left()-200);
                Mc.setRight(Mc.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
    case 8:                //赵云
        switch(Direction)
        {
        case 8:
            Zy.setTop(Zy.top()-200);
            Zy.setBottom(Zy.bottom()-200);
            if(Zy.intersects(B2)||Zy.intersects(Zf)||Zy.intersects(Mc)||Zy.intersects(Cc)||Zy.intersects(Hz)||Zy.intersects(B1)||Zy.intersects(Gy)||Zy.intersects(B3)||Zy.intersects(B4)||Zy.intersects(leftb)||Zy.intersects(topb)||Zy.intersects(bottomb)||Zy.intersects(rightb1)||Zy.intersects(rightb2)||Zy.intersects(b))
            {
                Zy.setTop(Zy.top()+200);
                Zy.setBottom(Zy.bottom()+200);
                steps--;
            }
            break;
        case 2:
            Zy.setTop(Zy.top()+200);
            Zy.setBottom(Zy.bottom()+200);
            if(Zy.intersects(B2)||Zy.intersects(Zf)||Zy.intersects(Mc)||Zy.intersects(Cc)||Zy.intersects(Hz)||Zy.intersects(B1)||Zy.intersects(Gy)||Zy.intersects(B3)||Zy.intersects(B4)||Zy.intersects(leftb)||Zy.intersects(topb)||Zy.intersects(bottomb)||Zy.intersects(rightb1)||Zy.intersects(rightb2)||Zy.intersects(b))
            {
                Zy.setTop(Zy.top()-200);
                Zy.setBottom(Zy.bottom()-200);
                steps--;
            }
            break;
        case 4:
            Zy.setLeft(Zy.left()-200);
            Zy.setRight(Zy.right()-200);
            if(Zy.intersects(B2)||Zy.intersects(Zf)||Zy.intersects(Mc)||Zy.intersects(Cc)||Zy.intersects(Hz)||Zy.intersects(B1)||Zy.intersects(Gy)||Zy.intersects(B3)||Zy.intersects(B4)||Zy.intersects(leftb)||Zy.intersects(topb)||Zy.intersects(bottomb)||Zy.intersects(rightb1)||Zy.intersects(rightb2)||Zy.intersects(b))
            {
                Zy.setLeft(Zy.left()+200);
                Zy.setRight(Zy.right()+200);
                steps--;
            }
            break;
        case 6:
            Zy.setLeft(Zy.left()+200);
            Zy.setRight(Zy.right()+200);
            if(Zy.intersects(B2)||Zy.intersects(Zf)||Zy.intersects(Mc)||Zy.intersects(Cc)||Zy.intersects(Hz)||Zy.intersects(B1)||Zy.intersects(Gy)||Zy.intersects(B3)||Zy.intersects(B4)||Zy.intersects(leftb)||Zy.intersects(topb)||Zy.intersects(bottomb)||Zy.intersects(rightb1)||Zy.intersects(rightb2)||Zy.intersects(b))
            {
                Zy.setLeft(Zy.left()-200);
                Zy.setRight(Zy.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
    case 9:                 //小兵1
        switch(Direction)
        {
        case 8:
            B1.setTop(B1.top()-200);
            B1.setBottom(B1.bottom()-200);
            if(B1.intersects(B2)||B1.intersects(Zf)||B1.intersects(Mc)||B1.intersects(Cc)||B1.intersects(Hz)||B1.intersects(Zy)||B1.intersects(Gy)||B1.intersects(B3)||B1.intersects(B4)||B1.intersects(leftb)||B1.intersects(topb)||B1.intersects(bottomb)||B1.intersects(rightb1)||B1.intersects(rightb2)||B1.intersects(b))
            {
                B1.setTop(B1.top()+200);
                B1.setBottom(B1.bottom()+200);
                steps--;
            }
            break;
        case 2:
            B1.setTop(B1.top()+200);
            B1.setBottom(B1.bottom()+200);
            if(B1.intersects(B2)||B1.intersects(Zf)||B1.intersects(Mc)||B1.intersects(Cc)||B1.intersects(Hz)||B1.intersects(Zy)||B1.intersects(Gy)||B1.intersects(B3)||B1.intersects(B4)||B1.intersects(leftb)||B1.intersects(topb)||B1.intersects(bottomb)||B1.intersects(rightb1)||B1.intersects(rightb2)||B1.intersects(b))
            {
                B1.setTop(B1.top()-200);
                B1.setBottom(B1.bottom()-200);
                steps--;
            }
            break;
        case 4:
            B1.setLeft(B1.left()-200);
            B1.setRight(B1.right()-200);
            if(B1.intersects(B2)||B1.intersects(Zf)||B1.intersects(Mc)||B1.intersects(Cc)||B1.intersects(Hz)||B1.intersects(Zy)||B1.intersects(Gy)||B1.intersects(B3)||B1.intersects(B4)||B1.intersects(leftb)||B1.intersects(topb)||B1.intersects(bottomb)||B1.intersects(rightb1)||B1.intersects(rightb2)||B1.intersects(b))
            {
                B1.setLeft(B1.left()+200);
                B1.setRight(B1.right()+200);
                steps--;
            }
            break;
        case 6:
            B1.setLeft(B1.left()+200);
            B1.setRight(B1.right()+200);
            if(B1.intersects(B2)||B1.intersects(Zf)||B1.intersects(Mc)||B1.intersects(Cc)||B1.intersects(Hz)||B1.intersects(Zy)||B1.intersects(Gy)||B1.intersects(B3)||B1.intersects(B4)||B1.intersects(leftb)||B1.intersects(topb)||B1.intersects(bottomb)||B1.intersects(rightb1)||B1.intersects(rightb2)||B1.intersects(b))
            {
                B1.setLeft(B1.left()-200);
                B1.setRight(B1.right()-200);
                steps--;
            }
            break;
        default:;
        }
        break;
     default:;
  }
    Direction=0;
    update();
}
void Klotski::keyPressEvent(QKeyEvent *event)
{
    if(iswin)
    {
        return;
    }
    QKeyEvent *key=(QKeyEvent*) event;
    timer=new QTimer(this);
        timer->start(0.1);
        connect(timer,SIGNAL(timeout()),SLOT(k_update()));
    switch(key->key())
    {
    case Qt::Key_0:
        role=0;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/兵选.wav");
          bx->play();
        }
        break;
    case Qt::Key_1:
        role=1;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/张选.wav");
          bx->play();
        }
        break;
    case Qt::Key_2:
        role=2;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/黄选.wav");
          bx->play();
        }
        break;
    case Qt::Key_3:
        role=3;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/兵选.wav");
          bx->play();
        }
        break;
    case Qt::Key_4:
        role=4;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/曹选.wav");
          bx->play();
        }
        break;
    case Qt::Key_5:
        role=5;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/关选.wav");
          bx->play();
        }
        break;
    case Qt::Key_6:
        role=6;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/兵选.wav");
          bx->play();
        }
        break;
    case Qt::Key_7:
        role=7;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/马选.wav");
          bx->play();
        }
        break;
    case Qt::Key_8:
        role=8;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/赵选.wav");
          bx->play();
        }
        break;
    case Qt::Key_9:
        role=9;
        if(isSound)
        {
          QSound *bx=new QSound(":/666/pic/兵选.wav");
          bx->play();
        }
        break;
    case Qt::Key_Up:Direction=8;
        if(role!=-1)
        steps++;
        break;
    case Qt::Key_W:Direction=8;
        if(role!=-1)
        steps++;
        break;
    case Qt::Key_Down:Direction=2;
        if(role!=-1)
        steps++;
        break;
    case Qt::Key_S:Direction=2;
        if(role!=-1)
        steps++;
        break;
    case Qt::Key_Left:Direction=4;
        if(role!=-1)
        steps++;
        break;
    case Qt::Key_A:Direction=4;
        if(role!=-1)
        steps++;
        break;
    case Qt::Key_Right:Direction=6;
        if(role!=-1)
        steps++;
        break;
    case Qt::Key_D:Direction=6;
        if(role!=-1)
        steps++;
        break;
    default:;
    }
}
void Klotski::restart()
{
   isrun=false;
   Direction=0;
   role=-1;
   steps=0;
   update();
}
void Klotski::openSound()
{
    isSound=true;
}
void Klotski::closeSound()
{
    isSound=false;
}
